package cn.com.cennavi.ICEAcess;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import cn.com.cennavi.bean.ODTaskSubmitInputParam;

/**
 * ice客户端任务提交请求参数转换为借口参数
 * 
 * @Description
 * @author buf
 * @date 2017年2月23日 下午1:58:14
 */
public class ICEClientInputFormat {
	public static void main(String[] args) {
		ODTaskSubmitInputParam inputParam = new ODTaskSubmitInputParam();
		inputParam.userID = "cennavi001";
		inputParam.algo = 101;// "odModel_odNum";
		int[] datases = new int[1];
		datases[0] = 010100;
		// datases[1] = 010200;
		inputParam.dataSource = datases;
		inputParam.startDateTime = "20160711010000";
		inputParam.endDateTime = "20160711232201";
		inputParam.areaFlag = 1;
		int[] areas = new int[1];
		areas[0] = 110000;
		inputParam.areas = areas;
		Map<java.lang.String, java.lang.String> algoParamMap = new HashMap<String, String>();
		algoParamMap.put("timegranularityFlag", "0");
		algoParamMap.put("timegranularity", "60");
		algoParamMap.put("spatialgranularityFlag", "0");
		algoParamMap.put("geoshapePath", "./xiamen_Shape/xiamenjiaotongxiaoqu.SHP");
		inputParam.algoParamMap = algoParamMap;
		inputParam.outputMode = 2;
		getService(inputParam);
	}

	public static void getService(ODTaskSubmitInputParam inputParam) {
		StringBuffer sb = new StringBuffer();

		// ----------------------------------------------------------------------------------//
		String userId = inputParam.userID;
		String modelId = null;
		String modelminId = null;
		switch (inputParam.algo) {
		case 101:
			modelId = "odModel";
			modelminId = "odNum";
			break;

		case 102:
			modelId = "odModel";
			modelminId = "hotSpot";
			break;
		}

		// -----------------------------获取
		// datasourse------------------------------//
		HashMap<String, Set<String>> dataSourseMap = getDataSourse(inputParam.dataSource);
		String dataSCBuf = "";
		for (Map.Entry<String, Set<String>> entry : dataSourseMap.entrySet()) {
			String dataSourse1 = entry.getKey();
			Set<String> dataClasses = entry.getValue();
			StringBuffer dataClassBuf = new StringBuffer();
			for (String dataClass : dataClasses) {
				dataSCBuf += dataSourse1 + ":" + dataClass;
				dataSCBuf += "|";
			}
		}
		String dataSC = dataSCBuf.substring(0, dataSCBuf.length() - 1);
		String[] scArr = dataSC.split("\\|");
		StringBuffer dataSourseSb = new StringBuffer();
		StringBuffer dataClassSb = new StringBuffer();
		for (int i = 0; i < scArr.length; i++) {
			String[] sca = scArr[i].split("\\:");
			dataSourseSb.append(sca[0]);
			dataClassSb.append(sca[1]);
			if (i != scArr.length - 1) {
				dataSourseSb.append("|");
				dataClassSb.append("|");
			}
		}
		System.out.println(dataSourseSb.toString());
		System.out.println(dataClassSb.toString());
		// -----------------------------------------------------------//
		String startDateTime = inputParam.startDateTime;
		String endDateTime = inputParam.endDateTime;
		System.out.println(startDateTime);
		System.out.println(endDateTime);
		// -----------------------------------------------------------//
		int spatialFlag = inputParam.areaFlag;
		int meshCode = -1;
		int countyCode = -1;
		int cityCode = -1;
		int provinceCode = -1;
		switch (inputParam.areaFlag) {
		case 0:
			countyCode = inputParam.areas[0];
			break;
		case 1:
			cityCode = inputParam.areas[0];
			break;
		case 2:
			provinceCode = inputParam.areas[0];
			break;
		case 3:
			meshCode = inputParam.areas[0];
			break;
		default:
			break;
		}
		System.out.println(meshCode);
		System.out.println(countyCode);
		System.out.println(cityCode);
		System.out.println(provinceCode);

		// ------------------------------------------------------------//
		String nowTimegranularityFlag = inputParam.algoParamMap.get("timegranularityFlag");
		int timegranularityFlag = Integer.parseInt(nowTimegranularityFlag == null ? "-1" : nowTimegranularityFlag);

		String nowTimegranularity = inputParam.algoParamMap.get("timegranularity");
		int timegranularity = Integer.parseInt(nowTimegranularity == null ? "-1" : nowTimegranularity);

		String nowspatialgranularityFlag = inputParam.algoParamMap.get("spatialgranularityFlag");
		int spatialgranularityFlag = Integer.parseInt(nowspatialgranularityFlag == null ? "-1" : nowspatialgranularityFlag);
		String geoshapePath = inputParam.algoParamMap.get("geoshapePath") == null ? "-1" : inputParam.algoParamMap.get("geoshapePath");
		int outflag = inputParam.outputMode == -1 ? 2 : inputParam.outputMode;

		String taskId = getTaskId(userId, modelId, modelminId, dataSourseSb.toString(), dataClassSb.toString(), startDateTime, endDateTime, outflag);
		System.out.println(taskId);
		inputParam.projectID = taskId;
		String outPath = "";
		switch (outflag) {
		case 1:
			// 输出到kafka，outFlagAndPath为topic
			outPath = "kafka://" + taskId;
			break;
		case 2:
			// 输出到hdfs
			outPath = "hdfs://nujhadoop/test/minlab/od/" + taskId;
			break;
		case 3:
			// 到mysql
			outPath = "mysql://" + taskId;
			break;
		default:

			break;
		}
		String cmd = "sh run.sh " + "'" + userId + "," + modelId + "," + modelminId + "," + dataSourseSb.toString() + "," + dataClassSb.toString() + "," + startDateTime + "," + endDateTime + "," + spatialFlag + "," + meshCode + "," + countyCode + "," + cityCode + "," + provinceCode + "," + timegranularityFlag + "," + timegranularity + "," + spatialgranularityFlag + "," + geoshapePath + "," + outflag + "," + outPath + "," + taskId + "'";
		System.out.println(cmd);

	}

	/**
	 * 生成任务id
	 * 
	 * @param modelminId
	 * @param modelId
	 * @param userId
	 * @param outPath
	 * @param endDateTime
	 * @param startDateTime
	 * @param dataClass
	 * @param dataSourse
	 * @param outflag
	 * @return
	 */
	private static synchronized String getTaskId(String userId, String modelId, String modelminId, String dataSourse, String dataClass, String startDateTime, String endDateTime, int outflag) {
		Date date = new Date();
		// String returnStr = userId + "_" + modelId + "_" + modelminId + "_" +
		// dataSourse + "_" + dataClass + "_" + startDateTime + "_" +
		// endDateTime +"_"+outflag+ "_" + date.getTime();
		String returnStr = userId + "_" + modelId + "_" + modelminId + "_" + date.getTime();

		return returnStr;
	}

	private static HashMap<String, Set<String>> getDataSourse(int[] dataSourceCode) {
		// TODO Auto-generated method stub
		HashMap<String, Set<String>> dataMap = new HashMap<String, Set<String>>();
		for (int nowCode : dataSourceCode) {
			String nowdataSourse = "";
			switch (nowCode) {
			case 010100:
				nowdataSourse = "trajectory_STL";
				break;
			case 010200:
				nowdataSourse = "trajectory_BAIDU";
				break;
			}
			String[] aa = nowdataSourse.split("\\_");
			String dataSourse = aa[0];
			String dataClass = aa[1];
			Set<String> v = dataMap.get(dataSourse);
			if (v == null) {
				v = new HashSet<String>();
				v.add(dataClass);
				dataMap.put(dataSourse, v);
			} else {
				v.add(dataClass);
			}

		}
		return dataMap;
	}
}
